package engine.control.loading
{
	import engine.control.MiniLoadingBar;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	
	import newx.com.loader.SwfLoader;

	/**
	 *队列加载器 
	 * @author yanghr
	 * 
	 */	
	public class MultLoader 
	{
		private static var _self:MultLoader;
		public static function getInstance():MultLoader
		{
			if(_self==null)
			{
				_self=new MultLoader(new privaceClass);
			}
			return _self;
		}
		private var _urlArr:Array;
		private var _maxlen:int;
		private var _currentLoadCount:int;
		private var _callback:Function;
		private var _plist:Array;
		private var _bar:MiniLoadingBar;
		private var _currentURL:String;
		
		private var _currentObj:Object=new Object;
		public function MultLoader(p:privaceClass)
		{
			_bar	=	MiniLoadingBar.getInstance();
		}
		/**
		 * 
		 * @param urlArr	完整url数组
		 * @param showBar  是否显示精度条
		 * @param callBack 全部加载完成回调函数
		 * @param plist		全部加载完成回调函数参数
		 * 
		 */		
		public function load(urlArr:Array,showBar:Boolean=true,callBack:Function=null,plist:Array=null):void
		{
			deletAllSource();
			_maxlen=urlArr.length;
			if(!urlArr.length)return ;
			_bar.autoHide=false;
			_urlArr=urlArr;
			_currentLoadCount=0;
			_callback=callBack;
			_plist=plist;
			if(showBar) _bar.show("Loading");
			loadNext();
		}
		private function loadSingle(url:String):void
		{
			SwfLoader.StartLoad(
				url,
				null,
				singleLoadComplete,
				[loadingHandle]);
		}
		private function loadingHandle(e:Event):void
		{
			var pe:ProgressEvent=e as ProgressEvent;
			if(pe)
			{
				_bar.progress=Number((pe.bytesLoaded/pe.bytesTotal*_currentLoadCount/_maxlen).toFixed(2));
				_bar.title	=	"资源加载中...... ";
			}
		}
		private function singleLoadComplete(val:DisplayObject,plist:Array=null):void
		{
			if (val != null )
			{
//				trace("MultLoader.singleLoadComplete(val, plist)加载成功",_currentURL);
//				ResManager.SaveRes(_currentURL, val);
				_currentObj[_currentURL]=val;
			}
			else
			{
//				trace("Error~MultLoader.singleLoadComplete(val, plist)加载失败",_currentURL);
			}
			loadNext()
		}
		/**
		 *检查加载器当前施法储存了 url为name的资源 
		 * @param name
		 * @return 
		 * 
		 */		
		public function hasSourceByUrl(name:String):Boolean
		{
			return _currentObj.hasOwnProperty(name);
		}
		/**
		 *取出url为name 的资源 
		 * @param name
		 * @return 
		 * 
		 */		
		public function getSourceByUrl(name:String):*
		{
			return _currentObj[name]
		}
		public function deletSourceByUrl(name:String):void
		{
			delete _currentObj[name];
		}
		/**
		 *删除全部资源 
		 * 
		 */		
		public function deletAllSource():void
		{
			for  (var i:String in _currentObj) 
			{
				deletSourceByUrl(i)
			}
			
		}
		private function loadNext():void
		{
			if(_urlArr.length!=0)
			{
				_currentURL=_urlArr.shift()
				loadSingle(_currentURL);
				_currentLoadCount++;
			}
			else
			{
				_bar.close();
				_bar.autoHide=true;
				_callback.apply(this,_plist);
			}
		}
		
	}
}
class privaceClass
{
	
}